package org.gc.webapp.action.admin.user;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.gc.common.vo.security.AdminUserDetailVO;
import org.gc.common.vo.security.RoleGroupVO;
import org.gc.common.vo.security.UserVO;
import org.gc.service.security.RoleGroupService;
import org.gc.service.security.UserService;
import org.gc.webapp.action.BaseAction;
import org.gc.webapp.util.BizUtil;
import org.springframework.security.providers.encoding.PasswordEncoder;


/**
 * 显示用户列表
 * 
 * @author bl00252
 * 
 */
public class UserAction extends BaseAction {

	private static final long serialVersionUID = -8520772634782353680L;

	private UserService userService;
	private RoleGroupService roleGroupService;
	private Long userid;
	private UserVO user;
	private List<UserVO> userList;
	private Set<Long> roleGroupIds;
	private List<RoleGroupVO> rolegroupList;
	private PasswordEncoder passwordEncoder;

	@Action("list1")
	public String list1() throws Exception {
		List<UserVO> list = userService.getAllNonAdminUser();
		Set<UserVO> set = new HashSet<UserVO>(list);
		setUserList(new ArrayList<UserVO>(set));
		return super.execute();
	}

	@Action("list")
	public String list() throws Exception {
		List<UserVO> list = userService.getAllAdminUser();
		Set<UserVO> set = new HashSet<UserVO>(list);
		setUserList(new ArrayList<UserVO>(set));
		return super.execute();
	}

	@Action(value = "edit")
	public String edit() throws Exception {

		rolegroupList = roleGroupService.getAll();
		Set<RoleGroupVO> set = new HashSet<RoleGroupVO>(rolegroupList);
		rolegroupList = new ArrayList<RoleGroupVO>(set);

		if (userid != null) {
			user = userService.get(userid);

			if (user != null) {

				if (user.getRoleGroups() != null) {
					roleGroupIds = new HashSet<Long>();
					for (RoleGroupVO vo : user.getRoleGroups()) {
						roleGroupIds.add(vo.getId());
					}
				}
			}
		}
		return SUCCESS;
	}

	@Action(value = "save", results = { @Result(name = "success", type = "redirectAction", params = {
			"actionName", "list" }),@Result(name = "input", location = "edit.jsp") })
	public String save() throws Exception {

		UserVO user2 = user;

		String department = user.getAdminUserDetail().getDepartment();
		String position = user.getAdminUserDetail().getPosition();

		if (userid != null) {
			user2 = userService.get(userid);
		} else {
			// 新用户
			user.setPassword(passwordEncoder.encodePassword(user.getPassword(),
					null));
		}

		if(user2.getAdminUserDetail()==null){
			user2.setAdminUserDetail(new AdminUserDetailVO());	
		}
		
		user2.getAdminUserDetail().setDepartment(department);
		user2.getAdminUserDetail().setPosition(position);
		user2.setEmail(user.getEmail());
		user2.setPhoneNumber(user.getPhoneNumber());
		user2.setMobile(user.getMobile());
		user2.setName(user.getName());
		user.setAdminUser(true);

		user2.setEnabled(user.isEnabled());

		roleGroupService.authorize(user2, roleGroupIds);
		saveMessage("用户信息[ " + user2.getUsername() + "(" + user2.getName()
				+ ")" + "]修改成功！");

		return super.execute();
	}

	@Action(value = "delete", results = { @Result(name = "success", type = "redirectAction", params = {
			"actionName", "list" }) })
	public String delete() {
		if (userid != null) {

			if (userid.equals(BizUtil.getLoginUser().getId())) {
				saveMessage("不能删自己！");
				return SUCCESS;
			} else {
				try {
					userService.remove(userid);
					saveMessage("删除成功！");
				} catch (Exception e) {
					saveMessage(e.getMessage());
				}
			}
		}
		return SUCCESS;
	}

	/**
	 * 重置密码
	 */
	@Action(value = "xpassword", results = {
			@Result(name = "success", type = "redirectAction", params = {
					"actionName", "list" }),
			@Result(name = "success1", type = "redirectAction", params = {
					"actionName", "list1" }) })
	public String xpassword() {
		if (userid != null) {

			if (userid.equals(BizUtil.getLoginUser().getId())) {
				saveMessage("不能给自己重置密码！");
				return SUCCESS;
			} else {
				try {
					UserVO user = userService.get(userid);

					String pass = userService.xpassword(userid);
					saveMessage("重置密码成功！用户[ " + user.getUsername() + "("
							+ user.getName() + ")" + "]的新密码为：" + pass);

					if (user.getAdminUser() == null
							|| user.getAdminUser().equals(false)) {
						return "success1";
					}
				} catch (Exception e) {
					saveMessage(e.getMessage());
				}
			}
		}
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setUserList(List<UserVO> userList) {
		this.userList = userList;
	}

	public List<UserVO> getUserList() {
		return userList;
	}

	public RoleGroupService getRoleGroupService() {
		return roleGroupService;
	}

	public void setRoleGroupService(RoleGroupService roleGroupService) {
		this.roleGroupService = roleGroupService;
	}

	public UserVO getUser() {
		return user;
	}

	public void setUser(UserVO user) {
		this.user = user;
	}

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}

	public List<RoleGroupVO> getRolegroupList() {
		return rolegroupList;
	}

	public void setRolegroupList(List<RoleGroupVO> rolegroupList) {
		this.rolegroupList = rolegroupList;
	}

	public Set<Long> getRoleGroupIds() {
		return roleGroupIds;
	}

	public void setRoleGroupIds(Set<Long> roleGroupIds) {
		this.roleGroupIds = roleGroupIds;
	}

	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

}
